<template>
    <div class="dragdesign-webPoster-container">
        <el-card shadow="hover" class="mb10" v-auth="'dragdesign:poster:list'">
            <el-input size="small" v-model="search.title" placeholder="请输入标题" prefix-icon="el-icon-search" style="max-width: 180px;" class="ml10"></el-input>
            <el-date-picker size="small" type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts" 
                unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" class="ml10"></el-date-picker>
            <el-button size="small" icon="el-icon-search" type="primary" class="ml10" @click="onTableSearch()">查询</el-button>
            <el-button size="small" icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
			<el-button v-auth="'dragdesign:poster:add'" size="small" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddWebPoster()">新增</el-button>
        </el-card>
        <el-card shadow="hover" ref="tableWebPosterRef">
			<div class="flex-warp" v-if="tableData.data.length > 0">
				<el-row :gutter="5">
					<el-col :xs="12" :sm="8" :md="6" :lg="4" :xl="2" class="mb15" v-for="(v, k) in tableData.data" :key="k" @click="onOpenDesignWebPoster(v)">
						<div class="flex-warp-item">
							<div class="flex-warp-item-box">
								<div class="item-img">
									<img v-if="v.cover" :src="v.cover"/>
									<div class="item-empty" v-else>
										<el-empty description="未上传封面图片" />
									</div>
								</div>
								<div class="item-txt">
									<div class="item-txt-title">
										{{ v.title }}
									</div>
									<div class="item-txt-other">
										<div style="width: 100%">
											<div class="item-txt-msg mb10">
												<span>评价 {{ v.evaluate || Math.floor(Math.random()*5+1) }}分</span>
												<span class="ml10">收藏 {{ v.collection || Math.floor(Math.random()*1000+1) }}</span>
											</div>
											<div class="item-txt-msg item-txt-price">
												<span class="font-price">
													<span>￥</span>
													<span class="font">{{ v.price || (Math.random()*200+1).toFixed(1) }}</span>
												</span>
												<span>月销{{ v.monSales || Math.floor(Math.random()*100+1) }}笔</span>
											</div>
										</div>
									</div>
								</div>
								<div class="oper-box">
									<i class="fa fa-eye ml10" aria-hidden="true" @click.stop.prevent="onOpenViewWebPoster(v)"></i>
									<i class="fa fa-magic ml10" aria-hidden="true" @click.stop.prevent="onOpenDesignWebPoster(v)"></i>
									<i class="fa fa-pencil-square-o ml10" aria-hidden="true" @click.stop.prevent="onOpenEditWebPoster(v)"></i>
									<i class="fa fa-trash-o ml10" aria-hidden="true" @click.stop.prevent="onTabelRowDel(v)"></i>
								</div>
							</div>
						</div>
					</el-col>
				</el-row>
			</div>
			<el-empty v-else description="暂无数据"></el-empty>
			<template v-if="tableData.data.length > 0">
				<el-pagination
					@size-change="onPageSizeChange"
					@current-change="onPageCurrentChange"
					class="mt15"
					:pager-count="5"
					:page-sizes="[12, 24, 48, 60, 72, 96]"
					v-model:current-page="search.pageNum"
					background
					v-model:page-size="search.pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					:total="tableData.total"
				>
				</el-pagination>
			</template>
        </el-card>
        <AddWebPoster ref="addWebPosterRef" @submit="onTableSearch"/>
        <EditWebPoster ref="editWebPosterRef" @submit="onTableSearch"/>
    </div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddWebPoster from '/@/views/dragdesign/poster/addWebPoster.vue';
import EditWebPoster from '/@/views/dragdesign/poster/editWebPoster.vue';
import { getWebPosterList, delWebPoster } from '/@/api/dragdesign/poster/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
import { useRouter } from 'vue-router';
export default {
    name: 'dragdesign/poster',
    components: { AddWebPoster, EditWebPoster },
    setup() {
		const router = useRouter()
        const tableWebPosterRef = ref();
        const addWebPosterRef = ref();
        const editWebPosterRef = ref();
        const state = reactive({
            search:{
                title: undefined,
                startTime: '',
                endTime: '',
                pageNum: 1,
                pageSize: 12,
            },
            dateData: {
                value: [],
                shortcuts: getDateRangeShortcuts()
            },
            tableData: {
                selections: [],//选中的行数据
                data: [],
                total: 0,
                loading: false
            }
        });

        const loadTableData = (params) => {
            getWebPosterList(Object.assign(state.search, params)).then((res) => {
                if(res.code == 0){
                    state.tableData.data = res.data;
                    state.tableData.total = res.total;
                    //tableWebPosterRef.value.clearSelection();
                }
            });
        }

        //查询
        const onTableSearch = () => {
            if(state.dateData.value.length == 2){
                state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
                state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
            }
            loadTableData(state.search);
        }
        //重置
        const onResetSearch = () => {
            state.dateData.value = [];
            state.search.title = undefined;
            state.search.startTime = undefined;
            state.search.endTime = undefined;
            state.search.pageNum = 1;
            state.search.pageSize = 10;
            loadTableData(state.search);
        }
        // 监听表格选中行
        const onTableSelectionChange = (selections) =>{
            state.tableData.selections = selections;
        };
        // 每页显示数据大小改变
        const onPageSizeChange = (val: number) => {
            state.search.pageSize = val;
            loadTableData(state.search);
        };
        // 页码改变
        const onPageCurrentChange = (val: number) => {
            state.search.pageNum = val;
            loadTableData(state.search);
        };
        // 打开新增弹窗
        const onOpenAddWebPoster = (row: object) => {
            addWebPosterRef.value.openDialog(row);
        };
        // 打开编辑弹窗
        const onOpenEditWebPoster = (row: object) => {
            row = row || state.tableData.selections[0];
            editWebPosterRef.value.openDialog(row);
        };
        // 删除选中行
        const onTabelRowsDel = () => {
            const ids = state.tableData.selections.map(item => item.id);
            if(ids && ids.length > 0){
                ElMessageBox.confirm('确定删除选中的WEB海报信息吗？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    delWebPoster(ids).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('删除成功');
                            loadTableData();
                        }
                    });
                }).catch(() => {});
            }
        };
        // 删除当前行
        const onTabelRowDel = (row: object) => {
            ElMessageBox.confirm('确定删除该条WEB海报信息吗？', '提示', {
                confirmButtonText: '删除',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                delWebPoster([row.id]).then((res) => {
                    if(res.code === 0){
                        ElMessage.success('删除成功');
                        loadTableData();
                    }
                });
            }).catch(() => {});
        };
		// 打开设计页面
		const onOpenDesignWebPoster = (row: object) => {
			router.push({
				path: '/decorator',
				query: { posterId : row.id, t: new Date().getTime() }
			});
		}
		const onOpenViewWebPoster = (row: object) => {
			window.open('/#/webapp?posterId=' + row.id);
		}
        // 页面加载时
        onMounted(() => {
            loadTableData();
        });
        return {
            tableWebPosterRef,
            addWebPosterRef,
            editWebPosterRef,
            onTableSearch,
            onResetSearch,
            onTableSelectionChange,
            onPageSizeChange,
            onPageCurrentChange,
            onOpenAddWebPoster,
            onOpenEditWebPoster,
            onTabelRowsDel,
            onTabelRowDel,
			onOpenDesignWebPoster,
			onOpenViewWebPoster,
            ...toRefs(state),
        };
    },
};
</script>
<style scoped lang="scss">
.flex-warp {
	display: flex;
	flex-wrap: wrap;
	align-content: flex-start;
	margin: 0 -5px;
	.flex-warp-item {
		padding: 5px;
		width: 100%;
		height: 360px;
		.flex-warp-item-box {
			border: 1px solid #ebeef5;
			width: 100%;
			height: 100%;
			border-radius: 2px;
			display: flex;
			flex-direction: column;
			transition: all 0.3s ease;
			&:hover {
				cursor: pointer;
				border: 1px solid var(--color-primary);
				transition: all 0.3s ease;
				box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.03);
				.item-txt-title {
					color: var(--color-primary) !important;
					transition: all 0.3s ease;
				}
				.item-img {
					img {
						transition: all 0.3s ease;
						transform: translateZ(0) scale(1.05);
					}
				}
			}
			.item-img {
				display: flex;
				width: 100%;
				height: 215px;
				overflow: hidden;
				img {
					transition: all 0.3s ease;
					width: 100%;
					height: 100%;
				}
				
				.item-empty{
					min-width: calc(100vw / 7.5);
					text-align: center;
					margin: 0 auto;
				}
			}
			.item-txt {
				flex: 1;
				padding: 8px;
				display: flex;
				flex-direction: column;
				overflow: hidden;
				.item-txt-title {
					text-overflow: ellipsis;
					overflow: hidden;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					display: -webkit-box;
					color: #666666;
					transition: all 0.3s ease;
					&:hover {
						color: var(--color-primary);
						text-decoration: underline;
						transition: all 0.3s ease;
					}
				}
				.item-txt-other {
					flex: 1;
					align-items: flex-end;
					display: flex;
					.item-txt-msg {
						font-size: 12px;
						color: #8d8d91;
					}
					.item-txt-price {
						display: flex;
						justify-content: space-between;
						align-items: center;
						.font-price {
							color: #ff5000;
							.font {
								font-size: 22px;
							}
						}
					}
				}
			}
			.oper-box{
				padding: 0 10px;
				line-height: 32px;
				color: #66b1ff;
				background-color: rgba(0,0,0,0.1);
				text-align: right;
				i{
					font-size: 16px;
				}
			}
		}
	}
}
</style>
